//
//  HDPasswordViewController.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


enum PwdType {
    case set, reset
}

class HDPasswordViewController: UIViewController ,UITextFieldDelegate{
    
    var regStr:PYPReg?
    let pwdView = HDIconTextFieldView()
    var setType:PwdType?
    var forgetVC:HDForgetPWViewController?
    var repwdStr = PYPRepwd()
    
    init(setType: PwdType) {
        super.init(nibName: nil, bundle: nil)
        self.setType = setType
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupViews()
        
    }
    override func viewWillDisappear(_ animated: Bool) {
        pwdView.textField.resignFirstResponder()
    }
    
    func finished() {
        print(pwdView.textField.text?.characters.count)
        if pwdView.textField.text == nil || pwdView.textField.text == "" {
            SVProgressHUD.showError("请输入密码")
            return
        }
        if pwdView.textField.text?.characters.count < 6 || pwdView.textField.text?.characters.count > 16 {
            SVProgressHUD.showError("请输入6-16位密码")
            return
        }
        if self.setType == .set {
            let pwd = pwdView.textField.text
            regStr?.ptxt = pwd?.md5()
            
            let identityVC = HDIdentificationViewController()
            identityVC.regStr = self.regStr
            navigationController?.pushViewController(identityVC, animated: true)
        } else {
            let phoneText = self.repwdStr.phone
            let codeText = self.repwdStr.code
            let newpText = pwdView.textField.text?.md5()
            let ckRepwd = PYPRepwd(phone: phoneText, newptxt: newpText, code: codeText)
            HDApiManagerHandler.requestRepwd(ckRepwd, success: { (dict) in
                print(dict)
                if dict["status"]?.int32Value == 1 {
                    let loginVC = HDLoginNewVC()
                    self.navigationController?.pushViewController(loginVC, animated: true)
                }else {
                    let msg = dict["msg"] as!String
                    SVProgressHUD.showError(msg)
                }
                }, failure: { (error) in
                    
            })
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        pwdView.textField.resignFirstResponder()
    }
    
    func setupViews() {
        if setType == .set {
            title = "设置密码"
        } else {
            title = "重设密码"
        }
        view.addSubview(pwdView)
        pwdView.icon.image = UIImage(named: "lock")
        pwdView.textField.placeholder = "请设置6-16位登录密码"
        pwdView.textField.clearButtonMode = .whileEditing
        pwdView.textField.isSecureTextEntry = true
        pwdView.snp.makeConstraints { (make) in
            make.top.equalTo(45)
            make.left.equalTo(view).offset(-1)
            make.right.equalTo(view).offset(1)
            make.height.equalTo(49)
        }
        
        let nextBtn = HDNextStepButton(title:"下一步")
        view.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { (make) in
            make.top.equalTo(pwdView.snp.bottom).offset(70)
            make.left.equalTo(view).offset(40)
            make.right.equalTo(view).offset(-40)
            make.height.equalTo(40)
        }
        nextBtn.addTarget(self, action: #selector(HDPasswordViewController.finished), for: .touchUpInside)
    }
}

